﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ditto.Models
{
    public class CommentRepository : ICommentRepository
    {
        DittoEntities db = new DittoEntities();

        public void DeleteComment(int id, Comment.CommentType type)
        {
            System.Data.Objects.DataClasses.EntityObject c;
            switch (type)
            {
                case Comment.CommentType.Item:
                    c = db.ItemComments.SingleOrDefault(i => i.ItemCommentID == id);
                    break;
                case Comment.CommentType.User:
                    c = db.UserComments.SingleOrDefault(i => i.UserCommentID == id);
                    break;
                case Comment.CommentType.Transaction:
                    c = db.TransactionComments.SingleOrDefault(i => i.TransactionCommentID == id);
                    break;
                default:
                    throw new ArgumentException();
            }

            if (c != null)
                db.DeleteObject(c);
        }

        public void Save()
        {
            db.SaveChanges();
        }

        public IEnumerable<Comment> FindItemComments(int itemID)
        {
            var comments = from c in db.ItemComments
                           where c.ItemID == itemID
                           orderby c.TimeCreated descending
                           select c;

            return comments.AsEnumerable().Select(c => ToComment(c));
        }

        public IEnumerable<Comment> FindUserComments(string userID)
        {
            var comments = from c in db.UserComments
                           where c.UserID == userID
                           orderby c.TimeCreated descending
                           select c;

            return comments.AsEnumerable().Select(c => ToComment(c));
        }

        public IEnumerable<Comment> FindTransactionComments(int transactionID)
        {
            var comments = from c in db.TransactionComments
                           where c.TransactionID == transactionID
                           orderby c.TimeCreated descending
                           select c;

            return comments.AsEnumerable().Select(c => ToComment(c));
        }

        public Comment FindTransactionFeedbackByTransactingUser(int transactionID, string transactingUserID)
        {
            var comment = db.TransactionComments.SingleOrDefault(t => t.TransactionID == transactionID && t.Transaction.TransactingUserID == transactingUserID && t.TransactionRating != null);

            if (comment == null)
                return null;
            else
                return ToComment(comment);
        }

        public Comment FindTransactionFeedbackByPostingUser(int transactionID, string postingUserID)
        {
            var comment = db.TransactionComments.SingleOrDefault(t => t.TransactionID == transactionID && t.Transaction.Item.PostingUserID == postingUserID && t.TransactionRating != null);

            if (comment == null)
                return null;
            else
                return ToComment(comment);
        }

        public IQueryable<Comment> FindCommentsByUser(string userID)
        {
            throw new NotImplementedException();
        }

        public void AddItemComment(Comment comment)
        {
            ItemComment c = ItemComment.CreateItemComment(0, Convert.ToInt32(comment.SubjectID), comment.IsPrivate, comment.By, comment.Message, comment.Time);
            db.ItemComments.AddObject(c);
        }

        public void AddUserComment(Comment comment)
        {
            UserComment c = UserComment.CreateUserComment(0, (string)comment.SubjectID, comment.IsPrivate, comment.By, comment.Message, comment.Time);
            db.UserComments.AddObject(c);
        }

        public void AddTransactionComment(Comment comment)
        {
            TransactionComment c = TransactionComment.CreateTransactionComment(0, Convert.ToInt32(comment.SubjectID), comment.IsPrivate, comment.By, comment.Message, comment.Time);
            db.TransactionComments.AddObject(c);
        }

        public void UpdateItemRating(Comment comment)
        {
            if (comment.Rating != null)
            {

            }

        }

        public void UpdateUserRating(Comment comment)
        {
            if (comment.Rating != null)
            {

            }
        }

        public void UpdateTransactionRating(Comment comment)
        {
            if (comment.Rating != null)
            {

            }
        }

        private Comment ToComment(ItemComment comment)
        {
            Comment result = new Comment(Comment.CommentType.Item, comment.ItemID);

            result.Message = comment.Comment;
            result.By = comment.CommentingUserID;
            result.Time = comment.TimeCreated;
            result.IsPrivate = comment.IsPrivate;
            result.Rating = comment.ItemRating;

            result.ID = comment.ItemCommentID;

            return result;
        }

        private Comment ToComment(TransactionComment comment)
        {
            Comment result = new Comment(Comment.CommentType.Item, comment.TransactionID);

            result.Message = comment.Comment;
            result.By = comment.CommentingUserID;
            result.Time = comment.TimeCreated;
            result.IsPrivate = comment.IsPrivate;
            result.Rating = comment.TransactionRating;

            result.ID = comment.TransactionCommentID;

            return result;
        }

        private Comment ToComment(UserComment comment)
        {
            Comment result = new Comment(Comment.CommentType.User, comment.UserID);

            result.Message = comment.Comment;
            result.By = comment.CommentingUserID;
            result.Time = comment.TimeCreated;
            result.IsPrivate = comment.IsPrivate;
            result.Rating = comment.UserRating;

            result.ID = comment.UserCommentID;

            return result;
        }

    }
}